﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class FPS : MonoBehaviour
{
    //摄像机
    public Transform agretctCamera;
    public bool allowFlying;
    public bool allowRunning;
    public float speed = 1;
    public float jumpPower = 5;
    public float gravity = 7f;
    public float mousespeed = 5f;
    public float maxmouseY = 45f;
    public float minmouseY = -45f;

    float RotationX = 0f;
    float RotationY = 0f;
    private CharacterController playerController;
    private Vector3 direction;
    bool isGrounded = false;

    private void Start()
    {
        RotationX += transform.eulerAngles.y;
        //LockCursor();
        playerController = GetComponent<CharacterController>();
    }

    public void TriggerEnter(Collider other)
    {
        isGrounded = true;
    }

    public void TriggerExit(Collider other)
    {
        isGrounded = false;
    }

    public void ResetGravity()
    {
        direction.y = -gravity * Time.deltaTime;
    }

    // Update is called once per frame
    private void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
            }
            else
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
            }
        } 

        //按下Esc键退出
        if (Input.GetKey(KeyCode.Escape))
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;

        }

        #region 移动与摄像跟随

        //获取上下坐标
        float _horizontal = Input.GetAxis("Horizontal");
        float _vertical = Input.GetAxis("Vertical");
        
        if (allowRunning && Input.GetKey(KeyCode.LeftShift))
        {
            _horizontal *= 10;
            _vertical *= 10;
        }
        direction = new Vector3(_horizontal, direction.y, _vertical);
        //判断是否在地板，获取移动的数据
        if (isGrounded || allowFlying)
        {
            if (Input.GetKeyDown(KeyCode.Space) || (allowRunning && Input.GetKey(KeyCode.Space)))
                direction.y = jumpPower / speed;
        }
        //调用CharacterController组件的移动方法
        CollisionFlags flags = playerController.Move(transform.TransformDirection(direction * Time.deltaTime * speed));
        if (flags == CollisionFlags.CollidedBelow)
        {
            direction.y = 0;
        }
        else 
        {
            //重力下坠
            direction.y -= gravity * Time.deltaTime;
        }
        //让摄像机旋转X轴跟Y轴
        RotationX += Input.GetAxis("Mouse X") * mousespeed;
        RotationY -= Input.GetAxis("Mouse Y") * mousespeed;
        //限制上下转动的角度
        RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);
        transform.eulerAngles = new Vector3(0, RotationX, 0);
        agretctCamera.transform.eulerAngles = new Vector3(RotationY, agretctCamera.transform.eulerAngles.y, 0);
        #endregion

    }



}
